package zozopo.background.operator.operations;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class QQOperatorOS extends OperatingService{
	
	//private static final Log logger = LogFactory.getLog(OperatorOS.class);
	
	public QQOperatorOS(Connection conn){
		super(conn);
		
	}
	
	public QQOperatorOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @author GEE
	 */
	
	public boolean isExistAccessToken(String access_token) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+QQOperatorTB.TABLE_NAME+" where "+QQOperatorTB.ACCESS_TOKEN+"='"+access_token+"'";
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	
	public boolean isRealAccessTokenNOpenid(String access_token,String openid) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+QQOperatorTB.TABLE_NAME+" where "+QQOperatorTB.OPENID+"='"+openid+"'"
		+ " AND " +QQOperatorTB.ACCESS_TOKEN +"='"+access_token+"'";
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	/**
	 * @author GEE
	 */
	
	public boolean isExistOpenId(String openid) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+QQOperatorTB.TABLE_NAME+" where "+QQOperatorTB.OPENID+"='"+openid+"'";
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @author GEE
	 */
	public List<QQOperatorTB> selectListBySql(String sql){
		//String sql = "select * from "+UserTB.TABLE_NAME+" order by  "+UserTB.USER_PRESTIGE+" desc limit "+beginPageNum+","+pageSize; 
		
		List<QQOperatorTB> list = null;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
			try {
				list = new ArrayList<QQOperatorTB>();
				while(resultSet.next()){
					QQOperatorTB qqOperatorTB = new QQOperatorTB();
					
					qqOperatorTB.setAccess_token(resultSet.getString(QQOperatorTB.ACCESS_TOKEN));
					
					qqOperatorTB.setEmail(resultSet.getString(QQOperatorTB.EMAIL));
					qqOperatorTB.setId_qqoperator(resultSet.getInt(QQOperatorTB.ID_QQOPERATOR));
					qqOperatorTB.setOpenid(resultSet.getString(QQOperatorTB.OPENID));
					qqOperatorTB.setOperatorid(resultSet.getInt(QQOperatorTB.OPERATORID));
					
					qqOperatorTB.setNickname(resultSet.getString(QQOperatorTB.NICKNAME));
					qqOperatorTB.setFigureurl(resultSet.getString(QQOperatorTB.FIGUREURL));
					qqOperatorTB.setFigureurl_1(resultSet.getString(QQOperatorTB.FIGUREURL_1));
					qqOperatorTB.setFigureurl_2(resultSet.getString(QQOperatorTB.FIGUREURL_2));
					qqOperatorTB.setGender(resultSet.getString(QQOperatorTB.GENDER));

					list.add(qqOperatorTB);
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return list;
	}
	
	/**
	 * @author GEE
	 */
	public QQOperatorTB getInfoByAccessToken(String access_token){
		QQOperatorTB operatorInfo = null;
		String sql = "select * from " + QQOperatorTB.TABLE_NAME + " where " +QQOperatorTB.ACCESS_TOKEN+"='"+access_token+"'";
		List<QQOperatorTB> list = selectListBySql(sql);
		if(list.size() >0){
			operatorInfo = list.get(0);
		}
		
		return operatorInfo;
	}
	
	/**
	 * @author gee
	 */
	public QQOperatorTB getInfoByEmail(String email){
		QQOperatorTB operatorInfo = null;
		String sql = "select * from " + QQOperatorTB.TABLE_NAME + " where " +QQOperatorTB.EMAIL+"='"+email+"'";
		List<QQOperatorTB> list = selectListBySql(sql);
		if(list.size() >0){
			operatorInfo = list.get(0);
		}
		
		return operatorInfo;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(QQOperatorTB qqoperator){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(QQOperatorTB.ACCESS_TOKEN, qqoperator.getAccess_token());
		
		insertMap.put(QQOperatorTB.EMAIL, qqoperator.getEmail());
		insertMap.put(QQOperatorTB.FIGUREURL, qqoperator.getFigureurl());
		insertMap.put(QQOperatorTB.FIGUREURL_1, qqoperator.getFigureurl_1());
		insertMap.put(QQOperatorTB.FIGUREURL_2, qqoperator.getFigureurl_2());
		insertMap.put(QQOperatorTB.GENDER, qqoperator.getGender());
		insertMap.put(QQOperatorTB.NICKNAME, qqoperator.getNickname());
		insertMap.put(QQOperatorTB.OPENID, qqoperator.getOpenid());
		insertMap.put(QQOperatorTB.OPERATORID, qqoperator.getOperatorid());
		
		return sqlMachine.insertRow(QQOperatorTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean update(QQOperatorTB qqoperator){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(QQOperatorTB.ACCESS_TOKEN, qqoperator.getAccess_token());
		
		updateMap.put(QQOperatorTB.EMAIL, qqoperator.getEmail());
		updateMap.put(QQOperatorTB.FIGUREURL, qqoperator.getFigureurl());
		updateMap.put(QQOperatorTB.FIGUREURL_1, qqoperator.getFigureurl_1());
		updateMap.put(QQOperatorTB.FIGUREURL_2, qqoperator.getFigureurl_2());
		updateMap.put(QQOperatorTB.GENDER, qqoperator.getGender());
		updateMap.put(QQOperatorTB.NICKNAME, qqoperator.getNickname());
		updateMap.put(QQOperatorTB.OPENID, qqoperator.getOpenid());
		updateMap.put(QQOperatorTB.OPERATORID, qqoperator.getOperatorid());
		
		return sqlMachine.updateRow(QQOperatorTB.TABLE_NAME, updateMap, QQOperatorTB.EMAIL, qqoperator.getEmail());
	}

}
